package com.controller;

import com.bean.ProjectTeacherBean;
import com.bean.ProjectUserBean;
import com.jfinal.core.Controller;
import com.model.Project;
import com.model.Teacher;
import com.model.TeacherGrade;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by chaiyuan on 2017/5/8.
 */
public class AdminController extends Controller {
    public void index(){render("/index_admin.jsp");}
    public void view() {
        renderJsp(getPara("page"));
    }

    public void audit_list(){
        List<Project> list = Project.dao.find("select * from project");
        List<ProjectTeacherBean> ptbl = new ArrayList<ProjectTeacherBean>();
        for (Project project:
            list ) {
            Teacher teacher  = Teacher.dao.findById(project.getInt("tea_id"));
            ProjectTeacherBean ptb = new ProjectTeacherBean();
            ptb.setProject(project);
            ptb.setTeacher(teacher);
            ptbl.add(ptb);
        }
        setAttr("list",ptbl);
        render("audit_list.jsp");
    }
    public void audit(){
        int project_id=getParaToInt(0);
        int status = getParaToInt(1);
        Project.dao.findById(project_id).set("status",status).update();
        render("audit_list.jsp");
    }
    public void add_people(){
        setAttr("project_list",Project.dao.find("select * from project where status = 1"));
        setAttr("teacher_list",Teacher.dao.find("select * from teacher"));
        render("add_people.jsp");
    }
    public void people_list(){
        List<ProjectTeacherBean> list = new ArrayList<ProjectTeacherBean>();
        List<TeacherGrade> TGList = TeacherGrade.dao.find("select * from teacher_grade");
        for (TeacherGrade tg:
            TGList ) {
            ProjectTeacherBean ptb = new ProjectTeacherBean();
            ptb.setTeacher(tg.getTeacher());
            ptb.setProject(tg.getProject());
            list.add(ptb);
        }
        setAttr("list",list);
        render("people_list.jsp");
    }
    public void addPeople(){
        new TeacherGrade().set("teacher_id",getParaToInt("teacher")).set("project_id",getPara("project")).save();
        redirect("/admin");
    }
}
